package com.aoxin.service.impl.course.webCourse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aoxin.mapper.SlfPriceHistoryMapper;
import com.aoxin.mapper.course.SlfSchoolCourseDtoMapper;
import com.aoxin.mapper.course.systemCourse.SlfSyllabusMapper;
import com.aoxin.mapper.course.webCourse.MyCourseMapper;
import com.aoxin.mapper.course.webCourse.SlfChildrenMapper;
import com.aoxin.model.SlfPriceHistory;
import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.course.SlfMyCourse;
import com.aoxin.model.dto.SchoolCourseDto;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.course.webCourse.MyCourseService;
import com.aoxin.service.inter.opm.UserService;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service
public class MyCourseServiceImpl implements MyCourseService {

	@Autowired
	private MyCourseMapper myCourseMapper;

	@Autowired
	private SlfPriceHistoryMapper slfPriceHistoryMapper;

	@Autowired
	private UserService userService;

	@Autowired
	private SlfSchoolCourseDtoMapper slfSchoolCourseDtoMapper;

	@Autowired
	private SlfSyllabusMapper slfSyllabusMapper;

	@Autowired
	private SlfChildrenMapper slfChildrenMapper;

	@Override
	@Transactional
	public Integer insertMyCourse(Integer courseId, Integer syllabusId, Integer childrenId) {
		Integer result = 0;
		Integer userId = ThreadLocalUtil.getUser().getUserId();
		Integer schoolId = ThreadLocalUtil.getChildren().getSchoolId();
		SlfChildren children = this.slfChildrenMapper.getAChildren(childrenId);
		if (syllabusId != null) {
			courseId = slfSyllabusMapper.getCourseId(syllabusId);
		}
		SchoolCourseDto schoolCourseDto = slfSchoolCourseDtoMapper.getSchoolCourse(schoolId, courseId);
		if (null == schoolCourseDto) {
			throw new IllegalArgumentException(ErrorUtil.SCHOOL_COURSE_NULL);
		}
		// 判断课程是否已近购买
		if (this.myCourseMapper.getCountMyCourse(childrenId, courseId) > 0) {
			throw new IllegalArgumentException(ErrorUtil.CHECK_HAS_RESULT);
		}
		// 判断余额
		SlfUser slfUser = userService.getUser(userId);
		if (slfUser.getLfb() < schoolCourseDto.getDiscount_price()) {
			throw new IllegalArgumentException(ErrorUtil.NOT_ENOUGH_BALANCE);
		}

		SlfMyCourse slfMyCourse = new SlfMyCourse();
		slfMyCourse.setUserId(userId);
		slfMyCourse.setUserName(ThreadLocalUtil.getUser().getName());
		slfMyCourse.setChildrenId(childrenId);
		slfMyCourse.setCourseId(schoolCourseDto.getId());
		slfMyCourse.setName(schoolCourseDto.getName());
		slfMyCourse.setNickName(schoolCourseDto.getNick_name());
		slfMyCourse.setDescription(schoolCourseDto.getDescription());
		slfMyCourse.setCourseType(schoolCourseDto.getCourse_type());
		slfMyCourse.setState(schoolCourseDto.getState());
		slfMyCourse.setUpDownState(schoolCourseDto.getUp_down_state());
		slfMyCourse.setParentId(schoolCourseDto.getParent_id());
		slfMyCourse.setSalesStartDate(schoolCourseDto.getSales_start_date());
		slfMyCourse.setSalesEndDate(schoolCourseDto.getSales_end_date());
		slfMyCourse.setEffectiveStartDate(schoolCourseDto.getEffective_start_date());
		slfMyCourse.setEffectiveEndDate(schoolCourseDto.getEffective_end_date());
		slfMyCourse.setFitAge(schoolCourseDto.getFit_age());
		slfMyCourse.setEndAge(schoolCourseDto.getEnd_age());
		slfMyCourse.setPrice(schoolCourseDto.getPrice());
		slfMyCourse.setDiscountPrice(schoolCourseDto.getDiscount_price());
//		slfMyCourse.setRemark("");
		result = myCourseMapper.insertMyCourse(slfMyCourse);
		if (result != 0) {
			if (slfUser.getLfb() >= slfMyCourse.getPrice()) {
				SlfPriceHistory slfPriceHistory = new SlfPriceHistory();
				Double lfb = slfUser.getLfb() - slfMyCourse.getPrice();
				slfUser.setUserId(userId);
				slfUser.setLfb(lfb);
				userService.updateUser(slfUser);
				slfPriceHistory.setUserId(userId);
				slfPriceHistory.setUserName(ThreadLocalUtil.getUser().getName());
				slfPriceHistory.setChildrenId(childrenId);
				slfPriceHistory.setChildrenName(children.getName());
				slfPriceHistory.setClassTypeId(schoolCourseDto.getType());
				slfPriceHistory.setCommodityId(schoolCourseDto.getId());
				slfPriceHistory.setCommodityName(schoolCourseDto.getName());
				slfPriceHistory.setReallyLfb(schoolCourseDto.getDiscount_price());
				slfPriceHistory.setDiscountLfb(schoolCourseDto.getDiscount_price());
				// TODO
//				Double discountPrice = schoolCourseDto.getDiscount_price();
//				if (null == discountPrice) {
//					discountPrice = 0D;
//				}
				slfPriceHistory.setShouldLfb(schoolCourseDto.getPrice());
				slfPriceHistory.setState(schoolCourseDto.getState());
				slfPriceHistoryMapper.insertSlfPriceHistory(slfPriceHistory);
			}
		}

		return result;
	}

	@Override
	public List<SlfMyCourse> getMyCourse(SlfMyCourse slfMyCourse) {
		List<SlfMyCourse> myCourseList = myCourseMapper.getMyCourse(slfMyCourse);
		return myCourseList;
	}

	@Override
	@Transactional
	public Integer deleteMyCourse(Integer myCourseId) {
		Integer result = 0;
		if (!EmptyUtil.isEmpty(myCourseId)) {
			result = myCourseMapper.deleteMyCourse(myCourseId);
		}
		return result;
	}

	@Override
	@Transactional
	public Integer deleteMyCourses(List<Integer> myCourseIds) {
		Integer result = 0;
		if (myCourseIds != null && myCourseIds.size() > 0) {
			result = myCourseMapper.deleteMyCourses(myCourseIds);
		}
		return result;
	}

	@Override
	@Transactional
	public Integer updateMyCourse(SlfMyCourse slfMyCourse) throws IllegalAccessException {
		Integer result = 0;
		if (!EmptyUtil.objFieldsIsEmpty(slfMyCourse)) {
			result = myCourseMapper.updateMyCourse(slfMyCourse);
		}
		return result;
	}

	@Override
	public List<SlfMyCourse> getMyChildrenCourse(Integer childrenId) {
		List<SlfMyCourse> slfMyCourseList = new ArrayList<>();
		if (childrenId != null) {
			SlfMyCourse slfMyCourse = new SlfMyCourse();
			slfMyCourse.setChildrenId(childrenId);
			slfMyCourseList = myCourseMapper.getMyCourse(slfMyCourse);
		}
		return slfMyCourseList;
	}

	@Override
	public List<Map<String, Object>> queryMyChildrencoursePage(Integer childrenId) {
		return myCourseMapper.queryMyChildrencoursePage(childrenId);
	}
	
	
	
	
	
	
	
	
	
}
